/**
 * FssRegistry.java
 */
package fss.remote;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.Observable;
import java.util.logging.Logger;

import fss.common.communication.Request;
import fss.util.FssUtils;
import fss.util.rmi.RmiId;

/**
 * A remote registry for service(s).
 * 
 * @author thinh
 *
 */
public class FssRegistry extends Observable implements ServiceRegistry {
    private static final long serialVersionUID = 1L;  
    /**
     * The logger for this class.
     */
    private final static Logger logger = Logger.getLogger(FssRegistry.class.getName());
    
    /**
     * The identifier for this remote service registry.
     */
    private RmiId id = null;
    /**
     * Constructor creating a service registry with specified id.
     * @param id the id.
     */
    public FssRegistry(RmiId id)  {
        this.id = id;
    }
    @Override
    public RmiId getId() throws RemoteException {
        return id;
    }
    @Override
    public void register(RemoteService service) throws RemoteException {
        service.setRegistryId(id); // this object is registered to this registry
        RemoteObserver ob = new RemoteObserver(service);
        addObserver(ob);
    }
    @Override
    public void processRequest(Request request) throws RemoteException {
        if(request == null) {
            logger.info("ServiceRegistry " + id + " received null request. Not processing.");
            return;
        }
        String message = FssUtils.formatDate(new Date()) + " ServiceRegistry " + id + " received request: " + request.getClass().getName() + " from " + request.getSource();
        logger.info(message);
        request.addLog(message);
        setChanged();
        notifyObservers(request);
        logger.info("All observer(s) notified.");
    }
    @Override
    public void stop() throws RemoteException {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void kill() throws RemoteException {
        // TODO Auto-generated method stub
        
    }
}
